package com.shop2cn.iapi.grouponmgr.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.grouponmgr.parameter.GrouponProductListBean;
import com.shop2cn.iapi.grouponmgr.parameter.SaveGrouponReq;
import com.shop2cn.iapi.grouponmgr.service.GrouponProductListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.Shop2cnGrouponCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.YMTDateUtil;

/**
 * 拼团商品列表Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GrouponProductList {
	private static GrouponProductListBean grouponproductlistBean;
	private static GrouponProductListCall grouponproductlistCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("拼团商品列表Test");
	}

	@Before
	public void caseUp() {
		grouponproductlistBean = new GrouponProductListBean();
		grouponproductlistCall = new GrouponProductListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(grouponproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_001() {
		Logger.start(true, "正确数据，查询拼团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			// SaveGrouponReq savegrouponBean =
			// Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
			// catalogList.get(0), 100d);
			// savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1,"yyyy-MM-dd
			// HH:mm:ss"));
			// savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3,"yyyy-MM-dd
			// HH:mm:ss"));
			// Integer grouponProductId =
			// Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil
					.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"), 10);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(0);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");
			JsonObject produ = grouponproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("records")
					.get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_002() {
		Logger.start(true, "正确数据，查询进行中的拼团");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(1);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");
			JsonObject produ = grouponproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("records")
					.get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_003() {
		Logger.start(true, "正确数据，查询未开始拼团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(2, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(2);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");
			JsonObject produ = grouponproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("records")
					.get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_004() {
		Logger.start(true, "正确数据，查询已结束拼团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil
					.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"), 3);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			Thread.sleep(5000);
			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(3);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");
			JsonObject produ = grouponproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("records")
					.get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_005() {
		Logger.start(true, "没有输入活动状态，默认查询所有状态");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil
					.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"), 10);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(0);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");
			JsonObject produ = grouponproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("records")
					.get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_006() {
		Logger.start(true, "没有输入商户编号");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			// grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(0);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("400", grouponproductlistCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_007() {
		Logger.start(true, "拼团被删除，查询为空");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil
					.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"), 3);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			Thread.sleep(5000);

			Shop2cnGrouponCallService.DeleteGroupon(grouponProductId, mchId);
			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(0);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");

			int total = grouponproductlistCall.getJsonObject().getAsJsonObject("data").get("total").getAsInt();
			Logger.verifyEquals(0, total, "判断查询为0");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_008() {
		Logger.start(true, "无效的活动状态");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil
					.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"), 10);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(6);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("400", grouponproductlistCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_009() {
		Logger.start(true, "商品被下架，查询为空");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			Thread.sleep(2000);
			AddAgentProductCallService.SetOffshelf(pid, mchId);

			Thread.sleep(2000);

			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(1);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");

			int total = grouponproductlistCall.getJsonObject().getAsJsonObject("data").get("total").getAsInt();
			Logger.verifyEquals(0, total, "判断查询为0");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponProductList_010() {
		Logger.start(true, "商品被下架，查询为空");
		try {
			//创建商品
			Integer mchId = 3383;
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerProductRequestV2(mchId);
			addProdReq.getProduct().setCatalogStatus(10);
			addProdReq.getProduct().setDeliveryCompanyType(1);
			addProdReq.getProduct().setDeliveryAging(5);
			String pid = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			//创建拼团
			SaveGrouponBean initSaveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(mchId, pid, addProdReq.getProduct().getCatalogList().get(0).getPrice()-10);
			ProductManagerCallServiceV3.SaveGrouponCall(initSaveGrouponBean);

			grouponproductlistBean.setSellerId(mchId);
			grouponproductlistBean.setStatus(0);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(100);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();

			Logger.verifyEquals("200", grouponproductlistCall.getString("code"), "验证code");
			JsonObject produ = grouponproductlistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("records")
					.get(0).getAsJsonObject();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}